package com.security.defend.wall;

import com.security.defend.wall.NativeLibrary;

import android.content.Context;

import androidx.annotation.NonNull;

import android.provider.Settings;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

/**
 * SecurityDefendWallPlugin
 */
public class SecurityDefendWallPlugin implements FlutterPlugin, MethodCallHandler {
    /// The MethodChannel that will the communication between Flutter and native Android
    ///
    /// This local reference serves to register the plugin with the Flutter Engine and unregister it
    /// when the Flutter Engine is detached from the Activity
    private MethodChannel channel;
    private Context context;

    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
        context = flutterPluginBinding.getApplicationContext();
        channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "security_defend_wall");
        channel.setMethodCallHandler(this);
    }

    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
        if (call.method.equals("getPlatformVersion")) {
            result.success("Android " + android.os.Build.VERSION.RELEASE);
        } else if (call.method.equals("isTampered")) {
            String signature = call.argument("signature");
            if (signature == null) {
                result.success(true);
                return;
            }
            SignatureChecker signatureChecker = new SignatureChecker();
            boolean isTampered = signatureChecker.isTampered(context, signature);
            result.success(isTampered);
        } else if (call.method.equals("isRooted")) {
            RootedChecker rootedChecker = new RootedChecker();
            boolean detected = rootedChecker.isRooted(context);
            result.success(detected);
        } else if (call.method.equals("isEmulator")) {
            EmulatorChecker emulatorChecker = new EmulatorChecker();
            boolean detected = emulatorChecker.isEmulator() || emulatorChecker.isPathEmulator();
            result.success(detected);
        } else if (call.method.equals("isDebugged")) {
            boolean detected = Settings.Global.getInt(context.getContentResolver(), Settings.Global.ADB_ENABLED, 0) == 1
                                || NativeLibrary.AntiFridaNativeLoader_checkFridaByPort();
            result.success(detected);
        } else {
            result.notImplemented();
        }
    }

    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
        channel.setMethodCallHandler(null);
    }
}
